#include "commify.h"
#include <string>
#include <boost/lexical_cast.hpp>

using std::string;

std::string commify(long long i, char delimiter)
{

    using boost::lexical_cast;

	string sign;
	if (i < 0L)
	{
		sign = '-';
		i = -i;
	}
	
	string s = lexical_cast<string>(i);
	string final_s;
	while (s.length() >= 4)
	{
		// remove three digits at a time from back
		final_s = delimiter + s.substr(s.length() - 3, 3) + final_s;
		s.erase(s.length() - 3,3);
	}
	return sign + s + final_s;
}

